import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Main',
    component: Main,
    children: [{
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home.vue')
      },
      {
        path: '/bookcity',
        name: 'Bookcity',
        component: () => import('../views/Bookcity.vue')
      },
      {
        path: '/share',
        name: 'Share',
        component: () => import('../views/Share.vue')
      },
      {
        path: '/mine',
        name: 'Mine',
        component: () => import('../views/Mine.vue')
      }
    ],
    redirect: '/home'
  },
  {
    path: '/detail:bookid',
    name: 'Detail',
    component: () => import( /* webpackChunkName: "about" */ '../views/Detail.vue')
  },
  {
    path: '/chapter:bookid',
    name: 'Chapter',
    component: () => import( /* webpackChunkName: "about" */ '../views/Chapter.vue')
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 让页面回到顶部
  document.documentElement.scrollTop = 0
  // 调用 next()，一定要调用 next 方法，否则钩子就不会被销毁
  next()
})

export default router